import { reactive } from "vue";
import type { FormRules } from "element-plus";

/** 自定义表单规则校验 */
export const formRules = reactive(<FormRules>{
  categoryName: [
    { required: true, message: "请输入类目名称", trigger: "blur" }
  ],
  categoryCode: [
    { required: true, message: "请输入类目编码", trigger: "blur" }
  ],
  parentId: [{ required: true, message: "请选择上级类目", trigger: "change" }],
  type: [{ required: true, message: "请选择商品类型", trigger: "change" }],
  sort: [
    {
      validator: (rule, value, callback) => {
        if (value === "") {
          callback(new Error("请输入"));
        } else if (isNaN(value)) {
          callback(new Error("请输入数字"));
        } else if (Number(value) < 1) {
          callback(new Error("需大于0"));
        } else {
          callback();
        }
      },
      required: true,
      trigger: "blur"
    }
  ]
});
